// pages/commentsshow/commentsshow.js

const config = require('../../config/config.js')
const commentCard = require('../../commentCard/commentCard.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    foodId: 0,
    comments: [],
    count: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      foodId: options.food_id
    })
    this.getComments()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getComments: function () {
    let that = this
    wx.request({
      url: `${config.requestBaseUrl}/Comment/getCommentsByFoodId`,
      method: 'POST',
      data: {
        food_id: that.data.foodId,
        limit: 0
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code === 1) {
          let data = res.data.data
          for (let i = 0, len = data.length; i < len; i++) {
            data[i]['create_time'] = data[i]['create_time'].trim().split(" ")[0]
            let imgGrids = []
            if (data[i]['comment_img1'] != null && data[i]['comment_img1'].length > 0) {
              imgGrids.push(data[i]['comment_img1'])
            }
            if (data[i]['comment_img1'] != null && data[i]['comment_img2'].length > 0) {
              imgGrids.push(data[i]['comment_img2'])
            }
            if (data[i]['comment_img1'] != null && data[i]['comment_img3'].length > 0) {
              imgGrids.push(data[i]['comment_img3'])
            }
            data[i]['imgs'] = imgGrids
          }
          that.setData({
            comments: data,
            count: res.data.count
          })
        }
      }
    })
  },

  previewCommentImg: function (e) {
    // console.log(e)
    let index = e.currentTarget.dataset.index
    let urls = e.currentTarget.dataset.imgs
    // console.log(urls)
    commentCard.previewCommentImg(index, urls)
  }




})